package edu.cmu.cs.stage3.alice.authoringtool.util;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class ProgressFrame extends JFrame {

 private javax.swing.Timer activityMonitor;
 private JProgressBar progressBar;
 private SimulatedActivity activity;

 public ProgressFrame () {
  setTitle(I18n.getString("ivprogInit")); // "iVProg init..."
  setSize(300, 200);
  addWindowListener (new WindowAdapter() {
   //Rem @Override
   public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
   } );

  Container contentPane = getContentPane();

  // set up panel with button and progress bar

  JPanel panel = new JPanel();
  progressBar = new JProgressBar();
  progressBar.setStringPainted(true);
  panel.add(progressBar);
  contentPane.add(panel, "South");

  activityMonitor = new javax.swing.Timer (500, new ActionListener () {
   public void actionPerformed(ActionEvent event){
    int current = activity.getCurrent();

    // show progress
    progressBar.setValue(current);

    // check if task is completed
    if (current == activity.getTarget()) {
     activityMonitor.stop();
     }
    }
   });

  progressBar.setMaximum(1000);
  activity = new SimulatedActivity(1000);
  activity.start();
  activityMonitor.start();
  }

 }

class SimulatedActivity extends Thread {

 private int current;
 private int target;

 public SimulatedActivity (int t) {
  current = 0;
  target = t;
  }

 public int getTarget () {
  return target;
  }

 public int getCurrent () {
  return current;
  }

 //Rem @Override
 public void run () {
  while (current < target && !interrupted()) {
   try {
    sleep(100);
   } catch (InterruptedException e) {
    return;
    }
   current++;
   }
  }

 }
